﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Diagnostics;


namespace PokerStarsClientTools
{
    /// <summary>
    /// Classe containing helper for teh UIAutomation repetitive task
    /// </summary>
    public class UIAutomationTools
    {

        /// <summary>
        /// Search for a elment just by name starting from root
        /// </summary>
        /// <param name="root"></param>
        /// <param name="name"></param>
        /// <param name="recursive"></param>
        /// <returns></returns>
        public static AutomationElement GetElement(AutomationElement root, string name, bool recursive)
        {
            PropertyCondition condName = new PropertyCondition(AutomationElement.NameProperty, name);
            return root.FindFirst(recursive ? TreeScope.Descendants : TreeScope.Children, condName);
        }


        /// <summary>
        /// Search for the first occurance of a given type of control
        /// </summary>
        /// <param name="root"></param>
        /// <param name="controlType"></param>
        /// <returns></returns>
        public static AutomationElement GetElement(AutomationElement root, ControlType controlType)
        {
            PropertyCondition condType = new PropertyCondition(AutomationElement.ControlTypeProperty, controlType);
            return root.FindFirst(TreeScope.Descendants, condType);
        }

        /// <summary>
        /// Search for a elment just by Classname starting from root
        /// </summary>
        /// <param name="root"></param>
        /// <param name="className"></param>
        /// <param name="recursive"></param>
        /// <returns></returns>
        public static AutomationElement GetElementByClassName(AutomationElement root, string className, bool recursive)
        {
            PropertyCondition classNameCond = new PropertyCondition(AutomationElement.ClassNameProperty, className);
            return root.FindFirst(recursive ? TreeScope.Descendants : TreeScope.Children, classNameCond);
        }

        /// <summary>
        /// Search for all elmenents just by Classname starting from root
        /// </summary>
        /// <param name="root"></param>
        /// <param name="className"></param>
        /// <param name="recursive"></param>
        /// <returns></returns>
        public static AutomationElementCollection GetElementsByClassName(AutomationElement root, string className, bool recursive)
        {
            PropertyCondition classNameCond = new PropertyCondition(AutomationElement.ClassNameProperty, className);
            return root.FindAll(recursive ? TreeScope.Descendants : TreeScope.Children, classNameCond);
        }


        ///// <summary>
        ///// Search for an elmenents just by Classname and Name starting from root
        ///// </summary>
        ///// <param name="root"></param>
        ///// <param name="className"></param>
        ///// <param name="recursive"></param>
        ///// <returns></returns>
        //public static AutomationElement GetElementsByClassName(AutomationElement root, string className, bool recursive)
        //{
        //    PropertyCondition classNameCond = new PropertyCondition(AutomationElement.ClassNameProperty, className);
        //    AutomationElementCollection elts= root.FindAll(recursive ? TreeScope.Descendants : TreeScope.Children, classNameCond);
        
        //}

        /// <summary>
        /// Walks the UI Automation tree and adds the control type of each enabled control 
        /// element it finds to a TreeView.
        /// </summary>
        /// <param name="rootElement">The root of the search on this iteration.</param>
        /// <param name="treeNode">The node in the TreeView for this iteration.</param>
        /// <remarks>
        /// This is a recursive function that maps out the structure of the subtree beginning at the
        /// UI Automation element passed in as rootElement on the first call. This could be, for example,
        /// an application window.
        /// CAUTION: Do not pass in AutomationElement.RootElement. Attempting to map out the entire subtree of
        /// the desktop could take a very long time and even lead to a stack overflow.
        /// </remarks>
        public static void WalkEnabledElements(AutomationElement rootElement)
        {
            Condition condition1 = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
            Condition condition2 = new PropertyCondition(AutomationElement.IsEnabledProperty, true);
            TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2));
            AutomationElement elementNode = walker.GetFirstChild(rootElement);
            while (elementNode != null)
            {
                //TreeNode childTreeNode = treeNode.Nodes.Add(elementNode.Current.ControlType.LocalizedControlType);

                Debug.WriteLine(elementNode.Current.ClassName + " " + elementNode.Current.Name);
                WalkEnabledElements(elementNode);
                elementNode = walker.GetNextSibling(elementNode);
            }
        }

    }
}
